/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "power_status.h"

#include "dfs_error.h"
#include "parameters.h"
#include "task_state_manager.h"
#include "utils_log.h"

namespace OHOS::FileManagement::CloudSync {
const std::string CLOUDFILESERVICEMODULE = "cloudfileservice";

void PowerStatusListener::SetDataSycner(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
{
    dataSyncManager_ = dataSyncManager;
}

uint32_t PowerStatusListener::GetPowerLevel()
{
    return powerLevel_;
}

void PowerStatusListener::OnPowerOverused(const std::string& module, uint32_t level)
{
    LOGI("OnPowerOverused module:%{public}s, level:%{public}d", module.c_str(), level);
    if (module == CLOUDFILESERVICEMODULE) {
        powerLevel_ = level;
        // SERIOUS is deadline
        if (level >= PowerStatus::SERIOUS) {
            LOGE("OnPowerOverused more than SERIOUS 100");
        }
    }
}

PowerStatus& PowerStatus::GetInstance()
{
    static PowerStatus instance;
    return instance;
}

void PowerStatus::InitPowerStatus(std::shared_ptr<CloudFile::DataSyncManager> dataSyncManager)
{
    LOGI("power status init");
    std::lock_guard<std::mutex> lock(initMutex_);
    if (listener_ == nullptr) {
        listener_ = OHOS::sptr<PowerStatusListener>::MakeSptr();
    }
    if (listener_ == nullptr) {
        LOGE("new powerstatus listener failed");
        return;
    }
    listener_->SetDataSycner(dataSyncManager);
    listener_->SetModuleName(CLOUDFILESERVICEMODULE);
    listener_->SetSubscriberName("POWER_USAGE");
    int ret = DevStandbyMgr::StandbyServiceClient::GetInstance().SubscribeStandbyCallback(listener_);
    if (ret != 0) {
        LOGE("subscribe power status listener callback failed ,code:%{public}d", ret);
        init_ = false;
    } else {
        init_ = true;
    }
}

void PowerStatus::UnInitPowerStatus()
{
    LOGI("power status uninit,init:%{public}d, listener is nullptr: %{public}d", init_, listener_ == nullptr);
    std::lock_guard<std::mutex> lock(initMutex_);
    if (init_&& listener_ != nullptr) {
        LOGI("power status already init");
        DevStandbyMgr::StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(listener_);
        init_ = false;
        listener_ = nullptr;
    } else {
        LOGI("power status have not init");
    }
}

uint32_t PowerStatus::GetPowerLevel()
{
    if (listener_ == nullptr) {
        LOGE("listener_ is nullptr, uninit");
        return 0;
    }
    uint32_t powerLevel = listener_->GetPowerLevel();
    LOGI("GetPowerLevel finish, powerLevel :%{public}d", powerLevel);
    return powerLevel;
}
}